package com.wswy.wzcx.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.che.libcommon.utils.input.DisableEmojiInputFilter;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.LayoutHelper;


/**
 * Created by wangduheng26 on 2017/12/18.
 * wangduheng26@gmail.com
 */

public class ChangeUserInfoFragment extends ILoginFragment {
    public static final String TAG = "ChangeUserInfoFragment";
    @Override
    public String getFragmentTitle() {
        return "昵称";
    }

    EditText editText;
    TextView tvTitle;
    public static ChangeUserInfoFragment newInstance() {

        Bundle args = new Bundle();

        ChangeUserInfoFragment fragment = new ChangeUserInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Context context = container.getContext();
        LinearLayout rootLayout = new LinearLayout(context);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        //rootLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.default_background_color));
        Space space = new Space(context);
        rootLayout.addView(space, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 20));


        LinearLayout outLayer = new LinearLayout(context);
        outLayer.setBackgroundColor(Color.WHITE);
        outLayer.setOrientation(LinearLayout.VERTICAL);
        outLayer.setPadding(AndroidUtilities.dp(15), AndroidUtilities.dp(10), AndroidUtilities.dp(15), AndroidUtilities.dp(6));

        tvTitle = new TextView(context);
        tvTitle.setMinHeight(AndroidUtilities.dp(24));
        tvTitle.setText("昵称");
        tvTitle.setGravity(Gravity.BOTTOM);
        outLayer.addView(tvTitle);


        LinearLayout layoutInput = new LinearLayout(context);
        layoutInput.setOrientation(LinearLayout.HORIZONTAL);

        editText = new EditText(context);
        editText.setBackgroundColor(Color.TRANSPARENT);
        editText.setInputType(InputType.TYPE_CLASS_TEXT);
        editText.setHint(getString(R.string.please_input_nickname));
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        editText.setTextColor(ContextCompat.getColor(context, R.color.gray20));
        editText.setHintTextColor(ContextCompat.getColor(context, R.color.hint_color));
        editText.setMaxEms(20);
        editText.setFilters(new InputFilter[]{new DisableEmojiInputFilter()});
        layoutInput.addView(editText, LayoutHelper.createLinear(0, LayoutHelper.WRAP_CONTENT, 1f));


        outLayer.addView(layoutInput, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));

        rootLayout.addView(outLayer, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));


        TextView textView = new TextView(context);
        textView.setText("请设置6-20位密码，不能有空格");
        textView.setPadding(AndroidUtilities.dp(15), AndroidUtilities.dp(12), 0, 0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        textView.setTextColor(ContextCompat.getColor(context, R.color.gray60));
        textView.setVisibility(View.GONE);
        rootLayout.addView(textView);

        return rootLayout;


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        editText.setHint(R.string.please_input_nickname);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                getActivity().invalidateOptionsMenu();
            }
        });


    }

    public String getText() {
        return String.valueOf(editText.getText());
    }

    @Override
    public void setupPage() {
        final DataCenter center = DataCenter.get();
        if (center.hasLogin()) {
            String nickName = center.getUserMode().nickName;
            editText.setText(nickName);
            editText.post(new Runnable() {
                @Override
                public void run() {

                    try {
                        Editable text = editText.getText();
                        if (!TextUtils.isEmpty(text)){
                            editText.setSelection(text.length());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

        }
    }
}
